/**
 * <p>Copyright: Copyright (c) 2014</p>
 * <p>Company: ---Technology Co. Ltd</p>
 * <p>2014-5-4</p>
 * @author Jan.Zhang
 */
package com.xianl.busiplat.dao.impl;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import com.xianl.busiplat.dao.BaseDao;
import com.xianl.busiplat.dwz.Page;


/** 
 * desc:
 * <p>author: Jan.Zhang </p>
 * date: 2014-5-4
 */
public class BaseDaoImpl<T> extends SqlSessionDaoSupport implements BaseDao<T> {

	protected static final String POSTFIX_FIND = ".Find";

	protected static final String POSTFIX_GET = ".Get";

	protected static final String POSTFIX_INSERT = ".Insert";

	protected static final String POSTFIX_UPDATE = ".Update";
	
	protected static final String POSTFIX_DELETE = ".Delete";
	
	protected Class<T> entityClass;
	protected String entityClassName;
	
	@SuppressWarnings("unchecked")
	public BaseDaoImpl(){
		entityClass = (Class<T>) getSuperClassGenricType(getClass(), 0);
		this.entityClassName = entityClass.getSimpleName();
	}
	
	@SuppressWarnings("rawtypes")
	private static Class getSuperClassGenricType(Class clazz, int index) {
		Type genType = clazz.getGenericSuperclass();
		if (!(genType instanceof ParameterizedType)) {
			return Object.class;
		}
		Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
		if (index >= params.length || index < 0) {
			return Object.class;
		}
		if (!(params[index] instanceof Class)) {
			return Object.class;
		}
		return (Class) params[index];
	}

	@Override
	public void insert(String statement, T t) {
		getSqlSession().insert(entityClassName+statement, t);
	}

	@Override
	public void insert(T t) {
		insert(POSTFIX_INSERT, t);
	}

	@Override
	public List<T> findAll(String statement, T t) {
		return getSqlSession().selectList(entityClassName+statement, t);
	}

	@Override
	public List<T> findAll(T t) {
		return findAll(POSTFIX_FIND, t);
	}

	@Override
	public List<T> findPage(String statement, Page page, Map params) {
		Integer totalCount = (Integer) getSqlSession().selectOne(entityClassName+statement+"_count",params);
		List<T> rsList = new ArrayList<T>();
		if (totalCount != null&& totalCount.intValue()>0){
			page.setTotalCount(totalCount);
			RowBounds rowBounds = new RowBounds((page.getPlainPageNum()-1)*page.getNumPerPage(), page.getNumPerPage());
			rsList = getSqlSession().selectList(entityClassName+statement, params, rowBounds);
		}
		return rsList;
	}

	@Override
	public List<T> findPage(Page page, Map params) {
		return findPage(POSTFIX_FIND, page, params);
	}

	@Override
	public T selectOne(String statement, T t) {
		return getSqlSession().selectOne(entityClassName+statement, t);
	}

	@Override
	public T selectOne(T t) {
		return selectOne(POSTFIX_GET, t);
	}

	@Override
	public void update(String statement, T t) {
		getSqlSession().update(entityClassName+statement, t);
	}

	@Override
	public void update(T t) {
		this.update(POSTFIX_UPDATE, t);
	}

	@Override
	public void delete(String statement, T t) {
		getSqlSession().delete(entityClassName+statement, t);
	}

	@Override
	public void delete(T t) {
		this.delete(POSTFIX_DELETE, t);
	}

	/* 
	 * desc:
	 * @see com.umpay.sc.plat.core.mapper.BaseMapper#clearCache()
	 */
	@Override
	public void clearCache() {
		getSqlSession().clearCache();
	}

	
}
